<template>
  <el-container>
    <el-header>
      <div>
        <img src="../assets/heima.png" alt />
        <span>某站后台系统</span>
      </div>
      <el-button type="info" @click="logout" class="loginOut">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="iscollapse ? '64px' : '200px'">
        <div class="foldMenu" @click="fold">|||</div>
        <!-- unique-opened用于只保持一个子菜单的展开 collapse用于折叠菜单 collapse-transition用于关闭折叠动画 default-active用于根据index值进行点击高亮 router用于根据index值进行页码跳转 -->
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409EFF" unique-opened :collapse="iscollapse" :collapse-transition="false" router :default-active="isActive">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单区域 -->
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
             <el-menu-item :index="'/' + items.path" v-for="items in item.children" :key="items.id" @click="saveNavState('/' + items.path)">
                <i class="el-icon-menu"></i>
               {{items.authName}}
               </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体内容 -->
      <el-main><router-view></router-view></el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menuList: [],
      // 不同的内容显示不同的字体图标
      iconObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      iscollapse: false,
      isActive: ''
    }
  },
  created() {
    this.getMenuList()
    // 页面一加载就赋值
    this.isActive = window.sessionStorage.getItem('active')
  },
  methods: {
    // 退出功能
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取数据
    async getMenuList() {
      const { data: res } = await this.axios.get('menus')
      if (res.meta.status !== 200) return this.msg.error(res.meta.msg)
      this.menuList = res.data
    },
    // 菜单折叠
    fold() {
      this.iscollapse = !this.iscollapse
    },
    // 点击二级菜单高亮
    saveNavState(active) {
      // 保存状态
      window.sessionStorage.setItem('active', active)
      this.isActive = active
    }
  }
}
</script>

<style scoped lang="less">
 .el-container {
   height: 100%;
 }
 .el-header{
   display: flex;
   justify-content: space-between;
   align-items: center;
   background-color:#373D41;
   > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
      font-size: 25px;
      color: #fff;
    }
   }
 }
 .el-aside{
  background-color:#333744;
 }
 .el-main{
  background-color:#eaedf1;
 }
 .el-menu {
   border-right: 0;
 }
 .foldMenu {
   height: 32px;
   background-color: #4A5064;
   line-height: 32px;
   text-align: center;
   color: #fff;
   font-size: 16px;
   letter-spacing: 0.2em;
   cursor: pointer;
 }
</style>
